{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6.1 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = {'Chinese':79, 'Math':80, 'English':93}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "79\n",
      "80\n",
      "93\n"
     ]
    }
   ],
   "source": [
    "print(scores['Chinese'])\n",
    "print(scores['Math'])\n",
    "print(scores['English'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6.1.1 创建字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用花括号{}创建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{201903001: '张虎', 201903002: '王丽', 201903003: '李明'}\n"
     ]
    }
   ],
   "source": [
    "student1_dict = {201903001:\"张虎\", 201903002:\"王丽\",201903003:\"李明\"}\n",
    "print(student1_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "先创建空字典再赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{201903001: '张虎', 201903002: '王丽', 201903003: '李明'}\n"
     ]
    }
   ],
   "source": [
    "student2_dict = {}\n",
    "student2_dict[201903001] = '张虎'\n",
    "student2_dict[201903002] = '王丽'\n",
    "student2_dict[201903003] = '李明' \n",
    "print(student2_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用dict()函数创建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{201903001: '张虎', 201903002: '王丽', 201903003: '李明'}\n"
     ]
    }
   ],
   "source": [
    "d_dict = dict() \n",
    "print(d_dict)\n",
    "d_dict[201903001]= '张虎'\n",
    "d_dict[201903002]= '王丽' \n",
    "d_dict[201903003]= '李明'\n",
    "print(d_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(201903001, 'Zhanghu'), (201903002, 'Wangli'), (201903003, 'Liming')]\n",
      "{201903001: 'Zhanghu', 201903002: 'Wangli', 201903003: 'Liming'}\n"
     ]
    }
   ],
   "source": [
    "stu_list = [(201903001,'Zhanghu'),(201903002,'Wangli'),(201903003,'Liming')]\n",
    "print(stu_list)\n",
    "student3_dict = dict(stu_list)\n",
    "print(student3_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "((201903001, 'Zhanghu'), (201903002, 'Wangli'), (201903003, 'Liming'))\n",
      "{201903001: 'Zhanghu', 201903002: 'Wangli', 201903003: 'Liming'}\n"
     ]
    }
   ],
   "source": [
    "stu_tuple = ((201903001,'Zhanghu'),(201903002,'Wangli'),(201903003,'Liming'))\n",
    "print(stu_tuple)\n",
    "student4_dict = dict(stu_tuple)\n",
    "print(student4_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'two': 2, 'one': 1, 'three': 3}\n"
     ]
    }
   ],
   "source": [
    "demo = [('two',2),('one',1),('three',3)]\n",
    "number = dict(demo)\n",
    "print(number)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'one': 1, 'two': 2, 'three': 3}\n"
     ]
    }
   ],
   "source": [
    "demokeys = ['one','two','three']\n",
    "demovalues = [1,2,3]\n",
    "number = dict(zip(demokeys,demovalues))\n",
    "print(number)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<zip at 0x29bafbcb608>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zip(demokeys,demovalues)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过函数fromkeys()创建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{201903001: None, 201903002: None, 201903003: None}\n"
     ]
    }
   ],
   "source": [
    "stu_num = [201903001,201903002,201903003]\n",
    "student5_dict = dict.fromkeys(stu_num)\n",
    "print(student5_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{201903001: 'Zhanghu', 201903002: 'Wangli', 201903003: 'Liming'}\n"
     ]
    }
   ],
   "source": [
    "student5_dict[201903001] = 'Zhanghu'\n",
    "student5_dict[201903002] = 'Wangli'\n",
    "student5_dict[201903003] = 'Liming'\n",
    "print(student5_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{201903001: None, 201903002: None, 201903003: None}\n",
      "{201903001: 'Zhanghu', 201903002: 'Wangli', 201903003: 'Liming'}\n"
     ]
    }
   ],
   "source": [
    "stu_num = (201903001,201903002,201903003)\n",
    "student6_dict = dict.fromkeys(stu_num)\n",
    "print(student6_dict)\n",
    "student6_dict[201903001] = 'Zhanghu'\n",
    "student6_dict[201903002] = 'Wangli'\n",
    "student6_dict[201903003] = 'Liming'\n",
    "print(student6_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6.1.2 访问字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 使用查找操作符[] 访问字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = {'Chinese':79, 'Math':80, 'English':93}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "79 80 93\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "252"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score1 = scores['Chinese']\n",
    "score2 = scores['Math']\n",
    "score3 = scores['English']\n",
    "print(score1,score2,score3) \n",
    "sum([score1,score2,score3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "各科成绩 252\n"
     ]
    }
   ],
   "source": [
    "print('各科成绩',sum([score1,score2,score3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = {'Chinese':79, 'Math':80, 'English':93}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'Physics'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-17-4ca3256d3c4f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mscore4\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mscores\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Physics'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mscore4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'Physics'"
     ]
    }
   ],
   "source": [
    "score4 = scores['Physics']\n",
    "print(score4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 使用函数get()获取字典指定键的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "各科总成绩 252\n"
     ]
    }
   ],
   "source": [
    "scores = {'Chinese':79, 'Math':80, 'English':93}\n",
    "score1 = scores.get('Chinese')\n",
    "score2 = scores.get('Math')\n",
    "score3 = scores.get('English')\n",
    "print('各科总成绩',sum([score1,score2,score3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "scores = {'Chinese':79, 'Math':80, 'English':93}\n",
    "print(scores.get('Physics',0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "99\n"
     ]
    }
   ],
   "source": [
    "scores = {'Chinese':79, 'Math':80, 'English':93, 'Physics':99}\n",
    "print(scores.get('Physics',0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.1.3 添加键值对"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1 通过赋值语句实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Chinese': 79, 'Math': 80, 'English': 93}\n",
      "{'Chinese': 79, 'Math': 80, 'English': 95, 'Physics': 85}\n"
     ]
    }
   ],
   "source": [
    "scores = {'Chinese':79, 'Math':80, 'English':93}\n",
    "print(scores)\n",
    "scores['Physics'] = 85\n",
    "scores['English'] = 95\n",
    "print(scores)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 2 使用函数setdefault()实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Chinese': 79, 'Math': 80, 'English': 93}\n"
     ]
    }
   ],
   "source": [
    "scores = {'Chinese':79, 'Math':80, 'English':93}\n",
    "print(scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Chinese': 79, 'Math': 80, 'English': 93, 'Physics': 85}\n",
      "{'Chinese': 79, 'Math': 80, 'English': 93, 'Physics': 85}\n",
      "85\n",
      "93\n"
     ]
    }
   ],
   "source": [
    "scores.setdefault('Physics',85)\n",
    "print(scores)\n",
    "scores.setdefault('English',95)\n",
    "print(scores)\n",
    "print(scores.setdefault('Physics',85))\n",
    "print(scores.setdefault('English',95))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17\n"
     ]
    }
   ],
   "source": [
    "dict = {'姓名':'张虎', '年龄':17}\n",
    "print(dict.setdefault('年龄',25))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NA\n",
      "{'姓名': '张虎', '年龄': 17, '性别': 'NA'}\n",
      "None\n",
      "{'姓名': '张虎', '年龄': 17, '性别': 'NA', '学校': None}\n"
     ]
    }
   ],
   "source": [
    "dict = {'姓名':'张虎', '年龄':17}\n",
    "print(dict.setdefault('性别','NA'))\n",
    "print(dict)\n",
    "print(dict.setdefault('学校'))\n",
    "print(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.1.4 修改字典中的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'语文': 79, '数学': 80, '英语': 93}\n"
     ]
    }
   ],
   "source": [
    "scores = {'语文': 79, '数学': 80, '英语': 93}\n",
    "print(scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'语文': 79, '数学': 80, '英语': 93}\n",
      "{'语文': 79, '数学': 80, '英语': 93, '程序设计': 99}\n"
     ]
    }
   ],
   "source": [
    "scores = {'语文': 79, '数学': 80, '英语': 93}\n",
    "print(scores)\n",
    "scores['程序设计'] = 99\n",
    "print(scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.1.5 删除键值对\n",
    "（1）使用函数del()实现；\n",
    "\n",
    "（2）使用函数pop()实现；\n",
    "\n",
    "（3）使用del语句实现。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'语文': 79, '数学': 80, '英语': 93}\n",
      "{'语文': 79, '数学': 80}\n"
     ]
    }
   ],
   "source": [
    "scores = {'语文': 79, '数学': 80, '英语': 93}\n",
    "print(scores)\n",
    "del(scores['英语'])\n",
    "print(scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'语文': 79, '数学': 80, '英语': 93}\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'物理'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-f3ff78ad088d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mscores\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m'语文'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;36m79\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'数学'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;36m80\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'英语'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;36m93\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mscores\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mdel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mscores\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'物理'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: '物理'"
     ]
    }
   ],
   "source": [
    "scores = {'语文': 79, '数学': 80, '英语': 93}\n",
    "print(scores)\n",
    "del(scores['物理'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'语文': 79, '数学': 80, '英语': 93}\n",
      "93\n",
      "{'语文': 79, '数学': 80}\n"
     ]
    }
   ],
   "source": [
    "scores = {'语文': 79, '数学': 80, '英语': 93}\n",
    "print(scores)\n",
    "print(scores.pop('英语'))\n",
    "print(scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'语文': 79, '数学': 80, '英语': 93}\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "scores = {'语文': 79, '数学': 80, '英语': 93}\n",
    "print(scores)\n",
    "print(scores.pop('物理',0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'语文': 79, '数学': 80, '英语': 93}\n",
      "{'语文': 79, '数学': 80}\n"
     ]
    }
   ],
   "source": [
    "scores = {'语文': 79, '数学': 80, '英语': 93}\n",
    "print(scores)\n",
    "del scores['英语']\n",
    "print(scores)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.1.6 遍历字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1 遍历字典的所有键值对；\n",
    "\n",
    "2 遍历字典中的所有键；\n",
    "\n",
    "3 遍历字典中的所有值。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1 遍历字典的所有键值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('语文', 79), ('数学', 80), ('英语', 93)])\n"
     ]
    }
   ],
   "source": [
    "scores = {'语文':79, '数学':80, '英语':93}\n",
    "print(scores.items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "语文成绩为: 79\n",
      "数学成绩为: 80\n",
      "英语成绩为: 93\n"
     ]
    }
   ],
   "source": [
    "scores = {'语文':79, '数学':80, '英语':93}\n",
    "for course,score in scores.items():\n",
    "    print(course+\"成绩为: \"+str(score))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2 遍历字典中的所有键"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['语文', '数学', '英语'])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores = {'语文':79, '数学':80, '英语':93}\n",
    "scores.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "语文\n",
      "数学\n",
      "英语\n"
     ]
    }
   ],
   "source": [
    "scores = {'语文':79, '数学':80, '英语':93}\n",
    "for course in scores.keys():\n",
    "    print(course)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "大学篮球是非必修课，成绩: 90分\n"
     ]
    }
   ],
   "source": [
    "scores = {'语文':79, '数学':80, '英语':93,'大学篮球':90}\n",
    "compulsory_course = ['语文', '数学', '英语']\n",
    "for course in scores.keys():\n",
    "    if course not in compulsory_course:\n",
    "        print(course+\"是非必修课，\"+\"成绩: \"+str(scores[course])+\"分\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "大学计算机不在scores字典中，请添加：\n",
      "语文 数学 英语 大学篮球 大学计算机 "
     ]
    }
   ],
   "source": [
    "scores = {'语文':79, '数学':80, '英语':93,'大学篮球':90}\n",
    "if '大学计算机' not in scores.keys():\n",
    "    print(\"大学计算机不在scores字典中，请添加：\")\n",
    "    scores['大学计算机']=96\n",
    "for course in scores.keys():\n",
    "    print(course,end=\" \")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3 遍历字典中的所有值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values([79, 80, 93, 90])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores = {'语文':79, '数学':80, '英语':93,'大学篮球':90}\n",
    "scores.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "79\n",
      "80\n",
      "93\n",
      "90\n"
     ]
    }
   ],
   "source": [
    "scores = {'语文':79, '数学':80, '英语':93,'大学篮球':90}\n",
    "for score in scores.values():\n",
    "    print(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.1.7 嵌套"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1 列表中嵌套字典；\n",
    "\n",
    "2 在字典中嵌套列表；\n",
    "\n",
    "3 在字典中嵌套字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1 列表中嵌套字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'语文': 79, '数学': 80, '英语': 93, '大学篮球': 90},\n",
       " {'语文': 75, '数学': 84, '英语': 97, '大学篮球': 93},\n",
       " {'语文': 77, '数学': 86, '英语': 96, '大学篮球': 94}]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores_list = []\n",
    "scores1 = {'语文':79, '数学':80, '英语':93,'大学篮球':90}\n",
    "scores2 = {'语文':75, '数学':84, '英语':97,'大学篮球':93}\n",
    "scores3 = {'语文':77, '数学':86, '英语':96,'大学篮球':94}\n",
    "scores_list.append(scores1)\n",
    "scores_list.append(scores2)\n",
    "scores_list.append(scores3)\n",
    "scores_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'语文': 79, '数学': 80, '英语': 93, '大学篮球': 90}\n",
      "{'语文': 75, '数学': 84, '英语': 97, '大学篮球': 93}\n",
      "{'语文': 77, '数学': 86, '英语': 96, '大学篮球': 94}\n"
     ]
    }
   ],
   "source": [
    "for score in scores_list:\n",
    "    print(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'语文': 79, '数学': 80, '英语': 93, '大学篮球': 90},\n",
       " {'语文': 75, '数学': 84, '英语': 97, '大学篮球': 93},\n",
       " {'语文': 77, '数学': 86, '英语': 96, '大学篮球': 94}]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores_list = []\n",
    "scores1 = {'语文':79, '数学':80, '英语':93,'大学篮球':90}\n",
    "scores2 = {'语文':75, '数学':84, '英语':97,'大学篮球':93}\n",
    "scores3 = {'语文':77, '数学':86, '英语':96,'大学篮球':94}\n",
    "scores_list = [scores1,scores2,scores3]\n",
    "scores_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2 在字典中嵌套列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "favorite_languages = {\n",
    "    '张虎':['python','ruby'],\n",
    "    '李四':['c'],\n",
    "    '王琳':['ruby','go'],\n",
    "    '马利':['python','haskell']\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "张虎喜欢多种语言，分别是：\n",
      "\tPython\n",
      "\tRuby\n",
      "\n",
      "李四喜欢的语言是：\n",
      "\tC\n",
      "\n",
      "王琳喜欢多种语言，分别是：\n",
      "\tRuby\n",
      "\tGo\n",
      "\n",
      "马利喜欢多种语言，分别是：\n",
      "\tPython\n",
      "\tHaskell\n"
     ]
    }
   ],
   "source": [
    "for name,languages in favorite_languages.items():\n",
    "    if len(languages)>1:\n",
    "        print(\"\\n\"+name.title()+\"喜欢多种语言，分别是：\")\n",
    "        for language in languages:\n",
    "            print(\"\\t\"+language.title())\n",
    "    else:\n",
    "        print(\"\\n\"+name.title()+\"喜欢的语言是：\")\n",
    "        for language in languages:\n",
    "            print(\"\\t\"+language.title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3 在字典中嵌套字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "用户名:201903001\n",
      "教务系统用户信息：用户名：201903001 姓名:张虎 系别:信科系 年级:2019级 专业计算机科学与技术\n",
      "\n",
      "用户名:201903002\n",
      "教务系统用户信息：用户名：201903002 姓名:王丽 系别:信科系 年级:2019级 专业大数据管理与应用\n"
     ]
    }
   ],
   "source": [
    "users = {\n",
    "    '201903001':{\n",
    "        '姓名':'张虎',\n",
    "        '系别':'信科系',\n",
    "        '年级':'2019级',\n",
    "        '专业':'计算机科学与技术'\n",
    "    },\n",
    "    '201903002':{\n",
    "        '姓名':'王丽',\n",
    "        '系别':'信科系',\n",
    "        '年级':'2019级',\n",
    "        '专业':'大数据管理与应用' \n",
    "    }\n",
    "}\n",
    "for username,user_info in users.items():\n",
    "    print(\"\\n用户名:\"+username)\n",
    "    xingming=user_info['姓名']\n",
    "    xibie=user_info['系别'] \n",
    "    nianji=user_info['年级']\n",
    "    zhuanye=user_info['专业']\n",
    "    print(\"教务系统用户信息：用户名：\"+username+' 姓名:'+xingming.title()\n",
    "         +' 系别:'+xibie+' 年级:'+nianji+' 专业'+zhuanye)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.1.8 常用字典内置函数 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "hash()函数的用法，并判断哪些数据类型可以作为字典的键"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{123: 'python'}\n"
     ]
    }
   ],
   "source": [
    "#参数是整数，整型123是可哈希的，可以作为字典的键\n",
    "hash(123)\n",
    "key_int_dict = {123:'python'}\n",
    "print(key_int_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'123': 'python'}\n"
     ]
    }
   ],
   "source": [
    "#参数是字符串，字符串'123'是可哈希的，可以作为字典的键\n",
    "hash('123')\n",
    "key_int_dict = {'123':'python'}\n",
    "print(key_int_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{(1, 2, 3): 'python'}\n"
     ]
    }
   ],
   "source": [
    "#参数是元组（没有可变数据类型），元组(1,2,3)是可哈希的，可以作为字典的键\n",
    "hash((1,2,3))\n",
    "key_int_dict = {(1,2,3):'python'}\n",
    "print(key_int_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-31-315391212e47>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-31-315391212e47>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    key_int_dict = {[1,2,3]:'python'}\u001b[0m\n\u001b[1;37m               ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "#参数是列表，抛出异常，提示列表是不可哈希的\n",
    "hash([1,2,3]\n",
    "key_int_dict = {[1,2,3]:'python'}\n",
    "print(key_int_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 应用举例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 0, 0, 1]\n",
      "nums:3   出现了:1\n",
      "nums:6   出现了:3\n",
      "nums:13  出现了:1\n",
      "nums:14  出现了:1\n",
      "nums:16  出现了:1\n",
      "nums:17  出现了:2\n",
      "nums:20  出现了:1\n"
     ]
    }
   ],
   "source": [
    "lst = [20, 16, 3, 13, 14, 6, 6, 6, 17, 17]\n",
    "count = [0] * 20\n",
    "for i in lst:\n",
    "    if count[i-1]!=0:\n",
    "        continue\n",
    "    count[i-1] = lst.count(i)\n",
    "print(count)\n",
    "for j in range(20):\n",
    "    if count[j]!=0:    \n",
    "        print(\"nums:{:<2}  出现了:{}\".format(j+1,count[j])) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20: 1\n",
      "16: 1\n",
      "3: 1\n",
      "13: 1\n",
      "14: 1\n",
      "6: 3\n",
      "17: 2\n"
     ]
    }
   ],
   "source": [
    "lst = [20, 16, 3, 13, 14, 6, 6, 6, 17, 17]\n",
    "dc = {}\n",
    "for i in lst:\n",
    "    if dc.get(i,0)==0:\n",
    "        dc[i]=1 \n",
    "    else:\n",
    "        dc[i]=dc.get(i)+1 \n",
    "for key,value in dc.items():\n",
    "    print(str(key)+\": \"+str(value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Good morning. Have a good class. Have a good visit. Have fun!'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text=\"Good morning. Have a good class. Have a good visit. Have fun!\"\n",
    "text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['good',\n",
       " 'morning',\n",
       " 'have',\n",
       " 'a',\n",
       " 'good',\n",
       " 'class',\n",
       " 'have',\n",
       " 'a',\n",
       " 'good',\n",
       " 'visit',\n",
       " 'have',\n",
       " 'fun']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text=\"Good morning. Have a good class. Have a good visit. Have fun!\"\n",
    "text=text.replace('.',' ')\n",
    "text=text.replace('!',' ')\n",
    "text=text.lower()\n",
    "words = text.split()\n",
    "words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "good: 3\n",
      "morning: 1\n",
      "have: 3\n",
      "a: 2\n",
      "class: 1\n",
      "visit: 1\n",
      "fun: 1\n"
     ]
    }
   ],
   "source": [
    "dc={}\n",
    "for i in words:\n",
    "    if dc.get(i,0)==0:\n",
    "        dc[i]=1 \n",
    "    else:\n",
    "        dc[i]=dc.get(i)+1 \n",
    "for key,value in dc.items():\n",
    "    print(str(key)+\": \"+str(value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'语文': 79, '数学': 80, '英语': 93, '大学篮球': 90}\n"
     ]
    }
   ],
   "source": [
    "scores1 = {'语文':79, '数学':80}\n",
    "scores2 = {'英语':93,'大学篮球':90}\n",
    "scores1.update(scores2)\n",
    "print(scores1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'dict_items' and 'dict_items'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-cc3eb5dd25fb>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mscores1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m'语文'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m79\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'数学'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m80\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mscores1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mscores2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'dict_items' and 'dict_items'"
     ]
    }
   ],
   "source": [
    "scores1 = {'语文':79, '数学':80}\n",
    "scores1.items()+scores2.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 集合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.2.1 创建集合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.创建可变集合           \n",
    "         ---(1)使用花括号{}创建可变集合。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'李云龙', '张明', '赵虎', '马汉', '王义'}\n"
     ]
    }
   ],
   "source": [
    "student_set = {\"张明\",\"赵虎\",\"马汉\",\"李云龙\",\"王义\"}\n",
    "print(student_set) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.创建可变集合           \n",
    "         ---(2)通过内置函数set() 创建可变集合。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'李云龙', '张明', '赵虎', '马汉', '王义'}\n"
     ]
    }
   ],
   "source": [
    "student_set = set([\"张明\",\"赵虎\",\"马汉\",\"李云龙\",\"王义\"])\n",
    "print(student_set) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'h', 'P', 'o', 'n', 't', 'y', ' '}\n"
     ]
    }
   ],
   "source": [
    "str_set = set('Python thon')\n",
    "print(str_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}\n"
     ]
    }
   ],
   "source": [
    "int_set = set(range(10))\n",
    "print(int_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'赵虎', '李云龙', '张明', '王义', '马汉'}\n"
     ]
    }
   ],
   "source": [
    "student_set = set([\"张明\",\"赵虎\",\"马汉\",\"李云龙\",\"王义\",\"赵虎\"])\n",
    "print(student_set) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.使用内置函数frozenset()创建不可变集合。           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "frozenset({'赵虎', '李云龙', '张明', '王义', '马汉'})\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'frozenset' object has no attribute 'add'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-15-c46c0135bce4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mstudent3_set\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfrozenset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"张明\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"赵虎\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"马汉\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"李云龙\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"王义\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstudent3_set\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mstudent3_set\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'兰轩'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'frozenset' object has no attribute 'add'"
     ]
    }
   ],
   "source": [
    "student3_set = frozenset([\"张明\",\"赵虎\",\"马汉\",\"李云龙\",\"王义\"])\n",
    "print(student3_set) \n",
    "student3_set.add('兰轩')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.2.2 访问集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "赵虎\n",
      "李云龙\n",
      "张明\n",
      "王义\n",
      "马汉\n"
     ]
    }
   ],
   "source": [
    "student1_set = {\"张明\",\"赵虎\",\"马汉\",\"李云龙\",\"王义\"}\n",
    "for student in student1_set:\n",
    "    print(student)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.2.3 更新集合（可变集合）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.添加元素 - add()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'李云龙', '张明', '赵虎', '马汉'}\n",
      "{'李云龙', '张明', '赵虎', '马汉', '王义'}\n"
     ]
    }
   ],
   "source": [
    "student1_set = {\"张明\",\"赵虎\",\"马汉\",\"李云龙\"}\n",
    "print(student1_set)\n",
    "student1_set.add('王义')\n",
    "print(student1_set)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.添加元素 - update()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'王义', '李云龙', '张明', '赵虎', '马汉'}\n"
     ]
    }
   ],
   "source": [
    "student1_set = {\"张明\",\"赵虎\",\"马汉\"}\n",
    "student2_set = {\"李云龙\",\"王义\"}\n",
    "student1_set.update(student2_set)\n",
    "print(student1_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.2.4 集合操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_set1 = {1,2,3,4}\n",
    "number_set2 = {3,4,5,6}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "number_set1 = {1,2,3,4}\n",
    "number_set2 = {3,4,5,6}\n",
    "print(number_set1 | number_set2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{3, 4}\n"
     ]
    }
   ],
   "source": [
    "number_set1 = {1,2,3,4}\n",
    "number_set2 = {3,4,5,6}\n",
    "print(number_set1 & number_set2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2}\n"
     ]
    }
   ],
   "source": [
    "number_set1 = {1,2,3,4}\n",
    "number_set2 = {3,4,5,6}\n",
    "print(number_set1 - number_set2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "number_set1 = {1,2,3,4}\n",
    "number_set2 = {3,4,5,6}\n",
    "print(number_set1 ^ number_set2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
